package QC.MessageBox.common;

import io.vertx.core.json.JsonArray;
import org.apache.commons.codec.binary.Hex;

import java.security.MessageDigest;
import java.util.*;

public class CodeUtil {
	
	public static String hexSHA1(String value) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			md.update(value.getBytes("utf-8"));
			byte[] digest = md.digest();
			return byteToHexString(digest);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}
	
	public static String byteToHexString(byte[] bytes) {
		return String.valueOf(Hex.encodeHex(bytes));
	}

	/**
	 * 对
	 * 		ArrayList
	 * 		HashMap
	 * 	检查容器是否为null或者空
	 * @param collection
	 * @return
	 */
	public static boolean collectionNotEmpty(Object collection) {
		if (collection == null) {
			return false;
		}
		if (collection instanceof ArrayList) {
			List list = (ArrayList)collection;
			if (list.size() > 0) {
				return true;
			} else {
				return false;
			}
		}
		if (collection instanceof HashMap) {
			Map map = (HashMap)collection;
			if (map.isEmpty()) {
				return false;
			} else {
				return true;
			}
		}
		if (collection instanceof JsonArray) {
			JsonArray array = (JsonArray) collection;
			if (array.isEmpty()) {
				return false;
			} else {
				return true;
			}
		}
		return false;
	}
}
